package com.zhentao.service.impl;

import cn.hutool.core.lang.UUID;
import com.zhentao.minio.service.impl.MinioFileStorageServiceImpl;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.service.ChargeUserFileService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.concurrent.TimeUnit;

import static com.zhentao.common.user.UserCommon.CHARGE_USER_AVATAR_PREFIX;
import static com.zhentao.common.user.UserCommon.CHARGE_USER_REDIS_PREFIX_AVATAR_IMG;

/**
 * @ClassName: ChargeUserFileServiceImpl
 * @Author: 振涛教育_Q
 * @Date: 2024/12/22 12:17
 */
@Service
public class ChargeUserFileServiceImpl implements ChargeUserFileService {

    @Autowired
    private MinioFileStorageServiceImpl fileStorageService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * @description  用户头像处理
     * @author 振涛教育_小Q
     * @date    2024/12/22 12:17
     * @param file
     * @return com.zhentao.model.common.dtos.ResponseResult
     */
    @Override
    @SneakyThrows
    public ResponseResult uploadAvatarImg(MultipartFile file) {
        // 先清除redis原有的头像地址
        redisTemplate.delete(CHARGE_USER_REDIS_PREFIX_AVATAR_IMG);
        // 头像处理
        String uuid = UUID.randomUUID().toString(true);
        String path = fileStorageService.uploadImgFile(CHARGE_USER_AVATAR_PREFIX, uuid + ".jpg", file.getInputStream());
        // 存储新的头像地址
        redisTemplate.opsForValue().set(CHARGE_USER_REDIS_PREFIX_AVATAR_IMG, path);
        // 设置过期时间
        redisTemplate.expire(CHARGE_USER_REDIS_PREFIX_AVATAR_IMG,60L, TimeUnit.SECONDS);
        return ResponseResult.okResult(200000,path);
    }
}
